package com.slipper.system.ai.service;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.ai.dao.AiFlowDao;
import com.slipper.system.ai.bo.AiFlowBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.Tool;
import com.slipper.util.UUID;

@Service
@ReadTransactional
public class AiFlowService {
    @Resource
    private AiFlowDao aiFlowDao;
    
    public AiFlowBO queryBy(String id) {
        return aiFlowDao.queryBy(id);
    }
    
    public List<AiFlowBO> queryList(AiFlowBO bo) {
        return aiFlowDao.queryList(bo);
    }
    
    public PageInfo<AiFlowBO> queryPage(AiFlowBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(aiFlowDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(AiFlowBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            return aiFlowDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            return aiFlowDao.insert(bo);
        }
    }

    @WriteTransactional
    public int saveJson(AiFlowBO bo) {
        return aiFlowDao.updateJson(bo);
    }

    @WriteTransactional
    public void saveList(List<AiFlowBO> list) {
        if (list == null) return;

        list.forEach(item -> {
            if (aiFlowDao.queryBy(item.getId()) != null) {
                item.setUpdateTime(System.currentTimeMillis());
                aiFlowDao.update(item);
            } else {
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                aiFlowDao.insert(item);
            }
        });
    }
    
    @WriteTransactional
    public int delete(AiFlowBO bo) {
        return aiFlowDao.delete(bo);
    }
}
